import { observable, action , toJS } from 'mobx';
import { Tools } from 'weaver-mobile';
import * as Api from '../../api/CommunicationApi';

import { Comment,Reply } from 'weaver-mobile-page';
const getLabel = Tools.getLabel;

export default class RelateStore {
  @observable itemsVO = {}; // 协作信息
  @observable dateType = 'all';
  @observable relatedPage = 1;
  @observable replyType = 'comment';
  @observable recordType = 'common';
  @observable replyVisible = null;  // 回复组件的显隐控制
  @observable replyComment = null;  // 点击的源数据
  @observable replyCommentParent = null;  // 点击事件的父级数据
  @observable relatedComment = new Comment.Store({
    getComment: this.discussRecordRelate,
    getChild: this.getChild,
    operate: this.detailOperate,
  });
  @observable quoteReply = new Reply.Store({ getAttachList: this.getReplyAttachList });
  @observable commentReplyStoreMap = {};
	@observable quoteReplyStoreMap = {};
	@observable currentModel = {}; //当前对话框弹窗

  @action('切换与我相关tabs')
  setDateType = x => {
    this.dateType = x;
    this.relatedPage = 1;
    this.relatedComment.getDatas();
  }

  @action setStoreState = (params) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    })
  }
  

  @action
  discussRecordRelate = () => {
    let defaultparams = {
      recordType: 'related',
      currentPage: this.relatedPage++,
      dateType :this.dateType
    }
    return Api.discussRecord(defaultparams).then(res => {
      return res;
    })
  }

  @action('评论组件的操作')
  detailOperate = params => {
    const { data, parent, type: { key, type } } = params;
    this.replyComment = data;
    this.replyCommentParent = parent;
    if (type === 'fabulous') { // 点赞
      this.coworkDiscussOperation(data, parent, 'agree');
    } else if (type === 'reply' || key === 'comment') { // 评论
      this.fixTop = this.fixTop;
      this.replyType = 'comment';
      if (!this.commentReplyStoreMap[data.replyId]) {
        this.commentReplyStoreMap[data.replyId] = new Reply.Store({ getAttachList: this.getReplyAttachList });
      }
      this.replyVisible(true);
    } else if (type === 'quote') {  // 引用---------------------待更改
      this.replyType = 'quote';
      if (!this.quoteReplyStoreMap[data.replyId]) {
        this.quoteReplyStoreMap[data.replyId] = new Reply.Store({ getAttachList: this.getReplyAttachList });
      }
      this.replyVisible(true);
    } else if (type === 'delete') {  // 删除
      //交流的删除
      if (key === 'delDiscuss'){
        this.currentModel = alert(getLabel(390724, '确认要删除该讨论记录'), '', [
         { text: getLabel(390722, '只删除该帖子'), onPress: () => {this.coworkDiscussOperation(data, parent, 'delDiscuss','',{isDeleteAll:0}) }},
         { text: getLabel(390721, '同时删除该帖子的所有评论'), onPress: () => {this.coworkDiscussOperation(data, parent, 'delDiscuss','',{isDeleteAll:1}) }},
         { text: getLabel(31129, '取消'), },
        ])
      //评论的删除  
      }else if(key === 'delComment'){
        this.currentModel = alert(getLabel(388758, '确认要删除吗？'),'', [
          { text: getLabel(31129, '取消') },
          {
            text: getLabel(33703, '确认'),
            onPress: () => {
                this.coworkDiscussOperation(data, parent, 'delComment');
            }
          },
        ])
      }
     
    } else if (key === 'dotop') { // 置顶
      this.relatedPage = 1;
      this.coworkDiscussOperation(data, parent, 'dotop', this.relatedComment.getDatas);
    } else if (key === 'canceltop') { // 取消置顶
      this.relatedPage = 1;
      this.coworkDiscussOperation(data, parent, 'canceltop', this.relatedComment.getDatas);
    } else if (key === 'doApproveDiscuss') { // 批准
      this.coworkDiscussOperation(data, parent, 'doApproveDiscuss');
    } else if (key === 'edit') { // 编辑
      this.replyType = 'reply';
      this.replyVisible(true);
    }
  }



  @action('协作-协作交流-置顶/取消置顶/审批/收藏/删除评论/赞 交流记录')
  coworkDiscussOperation = (data, parent, val, callback, otherparams) => {
    let params = {
      id: data.replyId,
      coworkId: data.coworkId,
      operationType: val,
      discussId: parent ? parent.replyId : '',
      ...otherparams,
    }
    return Api.coworkDiscussOperation(params).then(res => {
      if(val === 'delComment'){
        this.relatedComment.doOperateReply({
          replyId: data.replyId,
          parentId: parent ? parent.replyId : '',
          reply: res.replyList[0].childReply,
        })
      }else{
        this.relatedComment.doOperateReply({
          replyId: data.replyId,
          parentId: parent ? parent.replyId : '',
          reply: res.replyList[0]
        })
      }
      if (callback) callback();
    })
  }

  @action('评论提交事件')
  saveComment = (datas, callback) => {
    let atList = datas.atList;
    let params = {
      id: '',
      coworkId: (this.replyComment && this.replyComment.coworkId) || this.coworkId,
      remark: datas.text,
      relatedprj: '',
      relatedcus: '',
      relatedwf: '',
      relateddoc: '',
      relatedacc: '',
      projectIDs: '',
      replayid: this.replyComment ? this.replyComment.replyId : '',
      topdiscussid: this.replyComment ? (this.replyCommentParent ? this.replyCommentParent.replyId : this.replyComment.replyId) : '',
      isAnonymous: '',
      replyType: this.replyComment ? this.replyType : '',
      delrelatedacc: '',
      atUserIds: (toJS(atList) && toJS(atList).map(item => item.id)) || '',
    }
    return Api.submitCoworkDiscuss(params).then(res => {
      if (this.replyComment) {
        if('quote' === this.replyType){
          this.relatedPage = 1;
          this.relatedComment.getDatas();
          this.quoteReplyStoreMap[this.replyComment.replyId].doClear();
        }else{
          this.relatedComment.doOperateReply({
            replyId: this.replyComment.replyId,
            parentId: this.replyCommentParent ? this.replyCommentParent.replyId : '',
             reply: this.replyCommentParent ? res.replyList[0].childReply : res.replyList[0],
          })
          this.commentReplyStoreMap[this.replyComment.replyId].doClear();
        }
      } else {
        this.relatedPage = 1;
        this.relatedComment.getDatas();
        this.communicateReply.doClear();  // 发表完协作清除数据
      }
      this.replyVisible(false);
    })
  }

    
  getReplyAttachList = () => {
    let params = {
      routePath: this.routePath, 
      typeId: this.itemsVO.typeid || ''
    }
    return Api.getCoworkAppStatus(params).then(res => res);
  }

  @action
  setRoutePath = (pathname) => {
    this.routePath = pathname;
  }

  clear = () => {
    this.relatedPage = 1;
	}
	
	//关闭所有弹窗
	@action closeAllModel = () => {
		this.currentModel && typeof this.currentModel.close === 'function' && this.currentModel.close();
	}
} 